import {Injectable} from 'angular2/core';
import {EVT} from './mock-event';
import {LanmonEvent} from './lanmonevent';
import {Http, Response} from 'angular2/http';
import {Observable}     from 'rxjs/Observable';

@Injectable()
export class LanmonEventService {
    constructor (private http: Http) {}

    private _evtUrl = 'app/temp.json';
    //private _evtUrl = 'http://localhost:8080/getEventinfo';

  getEvt(){
        //console.log(this.http.get(this._evtUrl).map(res=>res.json));
        return EVT;
    }
    getEvtFromServer2():Observable<LanmonEvent>{
        return this.getEvtFromServer(this._evtUrl);
    }
    getEvtFromServer(_url:string):Observable<LanmonEvent>{
        //_url = this._evtUrl;
      //alert("url is "+_url);
        return this.http.get(_url).map(this.extractData)
          .catch(this.handleError)
    }

    private extractData(res: Response) {
       //alert("in extract data");
        if (res.status < 200 || res.status >= 300) {
            throw new Error('Bad response status: ' + res.status);
        }
        let body = res.json();
        return body || { };
    }
    private handleError (error: any) {
        // In a real world app, we might send the error to remote logging infrastructure
        let errMsg = error.message || 'Server error';
        console.error(errMsg); // log to console instead
        return Observable.throw(errMsg);
    }

}
